<?php
/**
 * NAME: updater
 * WHEN: OnMsg
 * PROVIDES: NONE
 * AUTOLOAD: true
 * DESCRIPTION: Zdalny updater tempaka
 * VERSION: 0.3
 * UPDATE: http://update.tempaq2.blacksoft.eu/check.php
 * AUTHOR: blacksoft.eu
 */
  
 exit();
 
//*module_register_start
	$cmd = $module->addCommand('!update', '_lang_update_desc', null, true);
	$cmd2 = $module->addSubcommand($cmd, 'check', '_lang_update_check_desc', null, true);
	$module->addSubcommand($cmd2, 'module', '_lang_check_module_desc', '.*', true);
	$module->addSubcommand($cmd2, 'modules', '_lang_check_modules_desc', null, true);
	$module->addSubcommand($cmd2, 'bot', '_lang_check_bot_desc', null, true);
	$module->addSubcommand($cmd, 'module', '_lang_update_module_desc', '.*', true);
	$module->addSubcommand($cmd, 'bot', '_lang_update_bot_desc', null, true);
	$module->addSubcommand($cmd, 'all', '_lang_update_all_desc', null, true);
	$bot->memo('last', false);
	
//*function_start
	if (isset($parsed_module) && count($parsed_module)) {
		if ($bot->is_admin($data->nick)) {
			$k = array_reverse(array_keys($parsed_module));
			$k = $k[0];
			$done = true;
			do {
				switch($k) {
					case 1:
						$k = 3;
						$done = false;
						break;
					case 2:
					case 3:
						$a = $bot->memo('last');
						if (isset($a) && is_array($a) && (time()-$a['timestamp']<30*60) && ($a['k'] === $k)) {
							$bot->sendarray($a['array']);
						} else {
							$m = (($k===3)?array_keys($bot->modulesinfo):explode(' ', $parsed_module[$k][0]));
							$array = array();
							if (isset($errors)) unset($errors);
							
							foreach ($m as $b) {
								if ($a = $bot->check_update($b)) {
									if ($k===2)
										$array[]=$lang->text('module_uptodate', $b);
								} elseif ($a === false) {
										$array[]=$lang->text('module_old', $b);
								} else {
									if ($k===2)
										$array[]=$lang->text('connection_error', $b);
									$errors[]=$b;
								}
							}
						//	var_dump($array);
							$bot->sendarray($array);
							
							if (isset($errors) && count($errors)) {
								$msg = $lang->text('errors_occured');
								foreach ($errors as $x) {
									$msg .= $x.', ';
								}
								$bot->send(substr($msg,0, strlen($msg)-2));
							} else {
								$bot->memo('last', array('timestamp' => time(), 'k'=>$k, 'array' => $array));
							}
						}
						if (!$done) $k = 4;
						break;
					case 4:
						if ($a = $bot->check_update('bot_core')) {
							$msg = $lang->text('core_uptodate');
						} elseif ($a === false) {
							$msg = $lang->text('new_version_available');
						} else {
							$msg = $lang->text('update_server_error');
						}
						$bot->send($msg);
						if (!$done) $done = true;
						break;
					case 5:
						$m = explode(' ', $parsed_module[$k][0]);
						foreach ($m as $a) {
							$plik = $bot->download_module($a);
							if (is_string($plik) && strlen($plik)) {
								//zrobmy najpierw backupa:)
								$m = $this->modulesinfo[$a];
								if (file_put_contents(MODULES_DIR.$m['filename'].'.backup', file_get_contents(MODULES_DIR.$m['filename'].'.php'))) {
									if (file_put_contents(MODULES_DIR.$m['filename'].'.php', $plik)) {
										$bot->send_text('update_saved', $a);
										$bot->load_module($m['filename'], $data, 1);
									}
								} else {
									//zle uprawnienia
									$bot->send_text('write_error');
								}
							}
						}
						break;
					case 6:
						$bot->send('Not implemented yet');
						break;
					case 7:
						$bot->send('Not implemented yet');
						break;
				}
			} while (!$done);
		}
	}
?>